package com.comp3111.happymeal;

import com.configuration.*;
import com.controller.UserFetch;
import com.controller.UserUpdate;
import com.object.User;
import com.user.*;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class LoginEditUserProfileActivity extends Activity {

	//Class Data
	EditText et_edit_profile_name;
	EditText et_edit_profile_description;
	ImageView iv_edit_profile_picture;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_profile);
        
        //Get previous got info
        Bundle extras = getIntent().getExtras();
		final String telephone = extras.getString("TELEPHONE");
		
		//Initialize Class Data for interface
		et_edit_profile_name = (EditText)findViewById(R.id.edit_profile_name);
		et_edit_profile_description = (EditText)findViewById(R.id.edit_profile_description);
		iv_edit_profile_picture = (ImageView)findViewById(R.id.edit_profile_picture);
		
		
		iv_edit_profile_picture.setOnClickListener(new ImageView.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
			}
		});
		
		
		//Userfetch
		UserFetch userFetch = new UserFetch();
		final User user = userFetch.fetchUserByTelephone(telephone);
		
		
		//Set the text based on the server data
		et_edit_profile_name.setText(user.getName());
		et_edit_profile_description.setText(user.getDescription());
		if(user.getProfilePicture() != ""){
			iv_edit_profile_picture.setTag(Configuration.baseURL + user.getProfilePicture());
			new DownloadImagesForImageView().execute(iv_edit_profile_picture);
		}
		
		//Configure the next button
		Button btn_edit_profile_next = (Button)findViewById(R.id.edit_profile_next);
				
		btn_edit_profile_next.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				
				// Store data to Owner Class static variables
				// TODO : Store correct value into the Class static variables
				Owner.id = user.getId();
				Owner.telephone = telephone;
				Owner.imei = LoginRegister.getImei(getApplicationContext());
				Owner.name = user.getName();
				Owner.description = user.getDescription();
				Owner.profile_picture = user.getProfilePicture();
				
				
				//Upload entered data to server
				UserUpdate userUpdate = new UserUpdate();
				final int FAIL = 0;
				if(FAIL == userUpdate.updateUserByTelephone(telephone,et_edit_profile_name.getText().toString(), et_edit_profile_description.getText().toString())){
					Toast.makeText(getApplicationContext(), "Unable to connect to server, please try again", Toast.LENGTH_LONG).show();
					return;
				}
				
				//Store to internal database
				DatabaseHandler db = new DatabaseHandler(getApplicationContext());
				Contact contact = new Contact(10,Owner.name,Owner.telephone);
				db.addContact(contact);
				
				
				//Jump to DiscoverPageActivity
				Intent intentPass = new Intent(LoginEditUserProfileActivity.this, DiscoverPageActivity.class);
				
				//Pass pass intent								
				//intentPass.putExtra("TELEPHONE",telephone.trim());
					
				//Go to next page
				startActivity(intentPass);
				finish();
			}
		});
	
		
		
		// TODO :
		// - Edit Photo
		// - Edit Username
		// - Edit Description
		// - Save userID and telephone to database
		// - Update photo, username and description to server
		 
    }
    
    
    /*
     * The app will not allow it to go back to LoginTypeSmsActivity through back
	 * Override the back button to prevent jumping back to LoadingScreenActivity
     */
	  
	  
	public void onBackPressed(){
		if(Configuration.showLog){
			Log.d("LoginEditUserProfileActivity", "onBackPressed");
		}
	}
}

